<template>
<div>
  <el-dialog title="编辑环境" width="40%" :destroy-on-close="true" :visible.sync="updateVisible" :before-close="handleClose">
    <el-form :model="form" :rules="rules" ref="ruleForm">
      <el-row :gutter="10">
        <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <span class="search-title">
                区域名称:
                </span>
            <div style="width:100%">
                <el-form-item prop="name" style="margin-bottom:0">
                    <el-input v-model="form.name" size="mini" placeholder="请输入区域名称" autocomplete="off"></el-input>
                </el-form-item>
            </div>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <span class="search-title">
                绿化信息:
                </span>
            <div style="width:100%">
                <el-form-item prop="security" style="margin-bottom:0">
                    <div class="content-item" v-for="(item,index) in green" :key="index">
                        <el-input v-model="item.name" size="mini" placeholder="绿化信息" autocomplete="off"></el-input>
                        <div v-if="index == (green.length - 1)" @click="addGreen" style="margin-left:10px"><i class="el-icon-circle-plus-outline"></i></div>
                        <div @click="minusGreen(index)" style="margin-left:10px" v-if="index != 0"><i class="el-icon-remove-outline"></i></div>
                    </div>
                </el-form-item>
            </div>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <span class="search-title">
                安保信息:
                </span>
            <div style="width:100%">
                <el-form-item prop="security" style="margin-bottom:0">
                    <div class="content-item" v-for="(item,index) in content" :key="index">
                        <div style="width:100px;margin-right:10px">
                          <el-input v-model="item.name" size="mini" placeholder="保安姓名" autocomplete="off"></el-input>
                        </div>
                        <el-input v-model="item.tel" size="mini" placeholder="联系方式" autocomplete="off"></el-input>
                        <div v-if="index == (content.length - 1)" @click="addContent" style="margin-left:10px"><i class="el-icon-circle-plus-outline"></i></div>
                        <div @click="minusContent(index)" style="margin-left:10px" v-if="index != 0"><i class="el-icon-remove-outline"></i></div>
                    </div>
                </el-form-item>
            </div>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <span class="search-title">
                备注:
                </span>
            <div style="width:100%">
                <el-form-item prop="remark" style="margin-bottom:0">
                    <el-input type="textarea" v-model="form.remark" size="mini" placeholder="请输入备注" autocomplete="off"></el-input>
                </el-form-item>
            </div>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="mini" type="primary" @click="submit">确 定</el-button>
      <el-button size="mini" @click="handleClose">取 消</el-button>
    </div>
  </el-dialog>
</div>
</template>

<script>
  import {editEnvironmentList,getEnvironmentListById} from '../../../api/api'
  export default {
    data() {
      return{
        content: [
            {
                name: "",
                tel: ""
            }
        ],
        green: [
          {
            name: "",
          }
        ],
        formLabelWidth: '80px',
        form: {
          name: "",
          security: "",
          remark: "",
          green: "",
        },
        rules: {
          name: [
            { required: true, message: '请输入区域名称', trigger: 'blur' },
          ],
        },
      }
    },
    props: ['updateVisible','updateId'],
    methods: {
      addContent() {
        var param = {
          name: "",
          tel: ""
        }
        this.content.push(param)
      },
      minusContent(index) {
        this.content.splice(index,1)
      },
      addGreen() {
        var param = {
          name: "",
        }
        this.green.push(param)
      },
      minusGreen(index) {
        this.green.splice(index,1)
      },
      submit() {
        this.$refs["ruleForm"].validate((valid) => {
          if (valid) {
            for (let index = 0; index < this.content.length; index++) {
                const element = this.content[index];
                if (!element.name) {
                    this.$message({
                        message: '请完善保安姓名',
                        type: 'warning'
                    });
                    return
                }
                if (!element.tel) {
                    this.$message({
                        message: '请完善保安联系方式',
                        type: 'warning'
                    });
                return
                }
            }
            this.form.security = JSON.stringify(this.content)
            for (let index = 0; index < this.green.length; index++) {
                const element = this.green[index];
                if (!element.name) {
                    this.$message({
                        message: '请完善绿化信息',
                        type: 'warning'
                    });
                    return
                }
            }
            this.form.green = JSON.stringify(this.green)
            editEnvironmentList(this.form).then(res => {
              if(res.code == 1000) {
                this.$notify.success({
                  title: '成功',
                  message: "保存成功"
                });
                this.handleClose();
              } else {
                this.$notify.error({
                  title: '错误',
                  message: res.message
                });
              }
            })
          } else {
            return false;
          }
        });
      },
      handleClose() {
        this.$emit("updateFalse")
      },
     
    },
    created() {
     
    },
    mounted() {
      
    },
    watch: {
      updateId(newVal) {
        if(newVal) {
          getEnvironmentListById({id:newVal}).then(res => {
            if(res.code == 1000) {
              this.form = res.data
              if (this.form.security) {
                this.content = JSON.parse(this.form.security)
                this.green = JSON.parse(this.form.green)
              }
            } else {
              this.$notify.error({
                title: '错误',
                message: res.message
              });
            }
          })
        }
        
      }
    }
 }
</script>

<style lang=scss scoped>
  .content-item {
    display: flex;
    align-items: center;
  }
  .el-col {
      display: flex;
      flex-direction: row;
      align-items: center;
      margin-top: 24px
  }
  .search-title {
      font-family: '黑体';
      float: right;
      white-space: nowrap;
      font-size: 14px;
      width: 84px;
      text-align: right;
  }
  .el-tree {
      border: 1px solid #BDC1C9;
  }
</style>